package biz;

import dao.ProductTypeDao;
import util.ProjectUitl;

import java.util.Map;

public class ProductTypeBiz {

    ProductTypeDao dao = new ProductTypeDao();

    public int saveProductType(String tname) throws BizException {

        if (tname == null || "".equals(tname)){
            throw new BizException("商品类型名不能为空");
        }

        String flag = dao.Check(tname).get("num").toString();

        System.out.println(flag);

        if (!"0".equals(flag)){
            throw new BizException("该商品类型已存在！");
        }

        return dao.saveProductType(tname);
    }

    // 商品类型 修改
    public int modifyProductType(String tname, String tid) throws BizException {

        if (tname == null || "".equals(tname)){
            throw new BizException("商品类型名不能为空");
        }

        return dao.modifyProductType(tname, tid);
    }

    public void saveMiniType(String name,String typedetailid,String tname) throws BizException{
        if (name == null || name.trim().isEmpty()==true){
            throw new BizException("类型名称不能为空");
        }

        String tid = null;
        final Map<String, Object> map = dao.findTid(tname);
        if (map!=null){
             tid= ProjectUitl.objToString(map.get("tid"));
        }else {
            throw new BizException("请选择主类名称");
        }

        if (typedetailid ==null || typedetailid.trim().isEmpty()==true){
            dao.InsertMiniType(name,tid);
        }else{
            dao.updateMiniType(name,typedetailid);
        }
    }
}
